﻿namespace Rio.Manager.Application.Views
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Rio.Manager.Application.ViewModel.Administration;
    using NetBay.Core.BusinessEntities.Connectors;
    using Telerik.Windows.Controls.GridView;
    using Telerik.Windows.Controls;
    using System.ComponentModel;
    using NetBay.Core.BusinessEntities.Barcodes;

    public partial class BarCodeTabView : UserControl
    {
        //Previous rows details selected.
        private object previousDetailsObject;

        #region Constructors

        public BarCodeTabView()
        {
            InitializeComponent(); 
        }
        
        #endregion Constructors
        
        #region Event

        /// <summary>Build the barcode list selected.</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadGridView_BarCode_Information_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            //Setter the barcode selected list.
            (DataContext as BarCodeTabViewModel).BarCodeInformationsSelected = new System.Collections.ObjectModel.ObservableCollection<Barcode>(RadGridView_BarCode_Information.SelectedItems.Cast<Barcode>());
            
            //Setter button properties.
            SetIsEnabledButton();
        }
        
        /// <summary>Show barcode details.</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadGridView_BarCode_Information_RowDetailsVisibilityChanged(object sender, GridViewRowDetailsEventArgs e)
        {
            //Only on details rows visible.
            if (e.Visibility == Visibility.Visible)
            {
                //Variable initialization.
                Barcode l_oCurrentItemToAdd = new Barcode();
                l_oCurrentItemToAdd = (Barcode)e.Row.Item;

                //Collapsed previous details rows. 
                if ((this.previousDetailsObject != null) && (previousDetailsObject != e.Row.Item))
                {
                    var previousRow = this.RadGridView_BarCode_Information.ItemContainerGenerator.ContainerFromItem(this.previousDetailsObject) as GridViewRow;
                    if (previousRow != null)
                    {
                        previousRow.DetailsVisibility = Visibility.Collapsed;
                    }
                }
                this.previousDetailsObject = e.Row.Item;

                //Update properties.
                if (l_oCurrentItemToAdd.Function == BarcodeSeparatorType.DocumentSeparator)
                {
                    (DataContext as BarCodeTabViewModel).RadTabItemSepDocumentIsEnabled = true;
                    (DataContext as BarCodeTabViewModel).RadTabItemSepFolderIsEnabled = false;
                    (DataContext as BarCodeTabViewModel).RadTabItemSepDocumentIsSelected = true;
                    (DataContext as BarCodeTabViewModel).RadTabItemSepFolderIsSelected = false;
                }
                else
                {
                    (DataContext as BarCodeTabViewModel).RadTabItemSepDocumentIsEnabled = false;
                    (DataContext as BarCodeTabViewModel).RadTabItemSepFolderIsEnabled = true;
                    (DataContext as BarCodeTabViewModel).RadTabItemSepDocumentIsSelected = false;
                    (DataContext as BarCodeTabViewModel).RadTabItemSepFolderIsSelected = true;
                }
            }
        }

        #endregion Event
       
        #region Method

        /// <summary>Set button properties.</summary>
        private void SetIsEnabledButton()
        {
            //Good properties button.
            switch ((DataContext as BarCodeTabViewModel).BarCodeInformationsSelected.Count())
            {
                case 0:                    
                    (DataContext as BarCodeTabViewModel).CanAddBarCode = false;
                    (DataContext as BarCodeTabViewModel).CanUpdateBarCode = false;
                    (DataContext as BarCodeTabViewModel).CanDeleteBarCode = false;
                    (DataContext as BarCodeTabViewModel).CanRefreshBarCode = false;
                    (DataContext as BarCodeTabViewModel).CanActivateBarCode = false;
                    break;

                case 1:
                    (DataContext as BarCodeTabViewModel).CanAddBarCode = true;
                    (DataContext as BarCodeTabViewModel).CanUpdateBarCode = true;
                    (DataContext as BarCodeTabViewModel).CanDeleteBarCode = true;
                    (DataContext as BarCodeTabViewModel).CanRefreshBarCode = true;
                    (DataContext as BarCodeTabViewModel).CanActivateBarCode = true;
                    break;

                default:
                    (DataContext as BarCodeTabViewModel).CanAddBarCode = true;
                    (DataContext as BarCodeTabViewModel).CanUpdateBarCode = false;
                    (DataContext as BarCodeTabViewModel).CanDeleteBarCode = true;
                    (DataContext as BarCodeTabViewModel).CanRefreshBarCode = true;
                    (DataContext as BarCodeTabViewModel).CanActivateBarCode = true;
                    break;
            }
        }

        #endregion Method 
    }
}